package com.jiyinit.web.core.common.util;

import com.jiyinit.core.framework.base.util.Check;
import com.jiyinit.core.framework.base.util.LogUtil;
import com.jiyinit.web.core.common.constant.Constant;
import com.jiyinit.web.core.common.vo.CurrentUserVo;
import com.jiyinit.web.core.common.vo.MobileUserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpSession;

/**
 * <p></p>
 * <p>
 * <PRE>
 * <BR>	修改记录
 * <BR>-----------------------------------------------
 * <BR>	修改日期			修改人			修改内容
 * </PRE>
 *
 * @author lishaochuan
 * @version 1.0
 * @date 2017/4/12 9:26
 * @since 1.0
 */
public class UserUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserUtil.class);

    private UserUtil() {

    }


    /**
     * 获取当前登录用户
     *
     * @param
     * @return
     * @author lishaochuan
     * @create 2017/4/12 9:27
     */
    public static CurrentUserVo getCurrentUser() {
        CurrentUserVo user = null;
        try {
            HttpSession session = HttpUtil.getSession();
            Object object = session.getAttribute(Constant.CURRENT_LOGIN_USER_NAME);

            if (!Check.isNullObj(object)) {
                user = (CurrentUserVo) object;
            }
        } catch (Exception e) {
            LogUtil.error(LOGGER, "Failed to get current user from the session ", e);
        }
        return user;
    }

    /**
     * 根据tokenId获取登录对象
     *
     * @param
     * @return
     * @author jack
     * @create 2017/4/12 9:27
     */
    public static MobileUserVo getMobileUser(String tokenId) {
        MobileUserVo user = null;
        try {
            HttpSession session = HttpUtil.getSession();
            Object object = session.getAttribute(tokenId);

            if (!Check.isNullObj(object)) {
                user = (MobileUserVo) object;
            }
        } catch (Exception e) {
            LogUtil.error(LOGGER, "Failed to get Moblie user from the session ", e);
        }
        return user;
    }

    /**
     * 往session中注入用户
     *
     * @param
     * @return
     * @author jack
     * @create 2017/4/12 9:29
     */
    public static void setMobileUserSession(MobileUserVo user,String tokenId) {
        HttpSession session = HttpUtil.getSession();
        session.setAttribute(tokenId, user);
    }


    /**
     * 往session中注入用户
     *
     * @param
     * @return
     * @author lishaochuan
     * @create 2017/4/12 9:29
     */
    public static void setUser2Session(CurrentUserVo user) {
        setUser2Session(user, HttpUtil.getSession());
    }

    /**
     * 往session中注入用户
     *
     * @param
     * @return
     * @author lishaochuan
     * @create 2017/4/12 9:29
     */
    public static void setUser2Session(CurrentUserVo user, HttpSession session) {
        session.setAttribute(Constant.CURRENT_LOGIN_USER_NAME, user);
    }

    /**
     * 从session中移除用户
     *
     * @param
     * @return
     * @author lishaochuan
     * @create 2017/4/12 9:29
     */
    public static void removeUserFromSession(HttpSession session) {
        session.removeAttribute(Constant.CURRENT_LOGIN_USER_NAME);
    }
}
